package com.juku2024.juku.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.juku2024.juku.R
import com.juku2024.juku.model.PlayInfoResponse
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.SPUtil
import java.util.*

class DialogSourceListAdapter(sources: ArrayList<PlayInfoResponse>):
    BaseQuickAdapter<PlayInfoResponse, BaseViewHolder>(R.layout.item_video_source, sources) {

    var sourceId: Int = 0
    init {
        addChildClickViewIds(R.id.btn_add_top_source)
    }

    override fun convert(holder: BaseViewHolder, item: PlayInfoResponse) {
        // 是否是当前播放的线路
        val isSelectedSource = sourceId == item.id
        // 文本颜色
        val color: Int
        val countColor: Int
        if (isSelectedSource){
            color = R.color.color_text_light
            countColor = R.color.color_text_light
        } else {
            color = R.color.app_text_white
            countColor = R.color.c999999
        }
        // 设置线路名称和集数
        holder.setText(R.id.tv_source_name, AppUtils.getSourceName(item.id) )
            .setText(R.id.tv_source_count, "(共${item.episodes.size}集)")
            .setTextColorRes(R.id.tv_source_name, color)
            .setTextColorRes(R.id.tv_source_count, countColor)

       // 设置下载图标
        holder.setGone(R.id.source_downloadStatus, !item.hasDownloads)

        // 设置置顶线路文本图案
        val isTopSource = item.id == SPUtil.instance.defaultTopSource
        if (isTopSource) {
            holder.setBackgroundResource(R.id.tv_top_source, R.drawable.shape_top_source)
                .setText(R.id.tv_top_source, "已置顶")
                .setTextColorRes(R.id.tv_top_source, R.color.cf1f1f1)
        } else {
            holder.setBackgroundResource(R.id.tv_top_source, R.drawable.shape_top_source_unselected)
                .setText(R.id.tv_top_source, "置顶")
                .setTextColorRes(R.id.tv_top_source, R.color.black)
        }

        // 推荐图标
        holder.setVisible(R.id.iv_source_recommend, item.isRecommend)


    }

}